﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GCELibrary.Current;

using System.Linq.Dynamic;
using System.Linq.Expressions;
using ZF.BLL.Linq;
using ZF.BLL;
using ZF.WS.Admin;
using ZF.Model;


namespace ZF.WS.Admin.shop
{
    public class freight : ZF.BLL.Page.MMPage
    {
        public override ModuleType Module
        {
            get { return ModuleType.S_Shop; }
            protected set
            {
                base.Module = value;
            }
        }
        public virtual string ListPage { get { return string.Format("{0}/shop/freightList.aspx", AppKey.SiteKey.ManageDirectory); } }
        public virtual string ManagePage { get { return string.Format("{0}/shop/freightManage.aspx", AppKey.SiteKey.ManageDirectory); } }
        public virtual string CTitle { get { return "快递价格模板"; } }
    }
    public partial class freightManage : freight
    {
        [PageArgument(Key = "id", CanNull = false, BindOperType = PageOperType.Edit)]
        public int  Id { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (this.OperType == PageOperType.AddNew)
                    Add();
                else if (this.OperType == PageOperType.Edit)
                    Edit();
            }
        }



        public void InitControls()
        {
            //var ptlist = DictionaryConfig.MTypeJsonList.Select(p => p.MType)
            //   .Where(p => p.ParentId == 0).Select(p => new SimpleTree<int>()
            //   {
            //       Id = p.MerchandiseTypeId,
            //       Name = p.Name,
            //       ParentId = 0,
            //   }).ToList();

            //DictionaryConfig.MTypeJsonList.Select(p => p.MType)
            //.ToList().ForEach(p =>
            //{
            //    if (p.ParentId != 0)
            //    {
            //        var parent = ptlist.FirstOrDefault(pt => pt.Id == p.ParentId);
            //        if (parent != null)
            //        {
            //            parent.Childs.Add(new SimpleTree<int>()
            //            {
            //                Id = p.MerchandiseTypeId,
            //                Name = p.Name,
            //                ParentId = p.ParentId,
            //            });
            //        }
            //    }
            //});
            //ptlist.ForEach(p =>
            //{
            //    this.ddlParams.Items.Add(new ListItem(p.Name, p.Id.ToString()));
            //    p.Childs.ForEach(c => this.ddlParams.Items.Add(new ListItem("└┈┈" + c.Name, c.Id.ToString())));
            //});

            //this.lvMTypeParams.DataSource = DictionaryConfig.MTypeJsonList;
            //this.lvMTypeParams.DataBind();
        }

        public void Add()
        {
            //InitControls();
            //this.fckInfo.Value = SiteInfo.SystemConfig.ShopTemplate;

        }
        public void AddSubmit(object sender, EventArgs e)
        {


            using (var dc = SiteInfo.CreateDBDataContext())
            {


                var freight = new ZF.Model.SP_Freight()
               {
                   Json = this.tbJson.Text,
                   Name = this.tbName.Text,

               };


                try
                {
                    dc.SP_Freight.InsertOnSubmit(freight);
                    dc.SubmitChanges();

                }
                catch (Exception ex)
                {
                    throw WarnFactory.CreateWarn(ex);
                }
                string msgKey = CTitle;

                SiteInfo.InsertLog("添加" + msgKey, null);
                WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回上一页", msgKey));
                link.AddWarnMessageLink(string.Format("继续添加{0}", msgKey), Request.Url.ToString());
                WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();

            }

        }

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                var freight = dc.SP_Freight.FirstOrDefault(p => p.FreightId == this.Id);
                if (freight == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                InitControls();
                this.tbJson.Text = freight.Json;
                this.tbName.Text = freight.Name;

            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {

                    var freight = dc.SP_Freight.FirstOrDefault(p => p.FreightId == this.Id);
                    if (freight == null)
                        throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                    freight.Name = this.tbName.Text;
                    freight.Json = this.tbJson.Text;

                    try
                    {
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    string msgKey = CTitle;

                    SiteInfo.InsertLog("修改" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回{0}列表", msgKey));

                    link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey), Request.Url.ToString());
                    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();

                }


            }

        }

    }
}
